package com.xiaoleng.tlgsku.service.Impl;

import com.xiaoleng.tlgsku.dao.AttributeKeyMapper;
import com.xiaoleng.tlgsku.pojo.AttributeKey;
import com.xiaoleng.tlgsku.service.AttributeKeyService;
import com.xiaoleng.tlgsku.tools.Record;
import com.xiaoleng.tlgsku.tools.Status;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class AttibuteKeyServiceImpl implements AttributeKeyService {

    @Autowired
    private AttributeKeyMapper attributeKeyMapper;

    @Override
    public Record findValuekeyBygoodsId(Integer goodsId) {
        Record record = new Record();
        try {
            List<AttributeKey> list = attributeKeyMapper.findValuekeyBygoodsId(goodsId);
            if (null != list && list.size() != 0) {
                record.setStatus(Status.SELECT_SUCCESS);
                record.setMsg("查询规格成功！");
                record.setData(list);
            } else {
                record.setStatus(Status.SELECT_FALIED);
                record.setMsg("查询规格失败！");
            }
        } catch (Exception e) {
            record.setStatus(Status.SELECT_FALIED);
            record.setMsg("查询规格异常！");
            e.printStackTrace();
        } finally {
            return record;
        }

    }

    @Override
    public Record addKey(AttributeKey attributeKey) {
        Record record = new Record();
        int count = 0;
        try {
            count = attributeKeyMapper.addKey(attributeKey);
            if (count > 0) {
                record.setStatus(Status.SAVE_SUCCESS);
                record.setMsg("添加key成功！");
            } else {
                record.setStatus(Status.SAVE_FALIED);
                record.setMsg("添加key失败！");
            }
        } catch (Exception e) {
            record.setStatus(Status.SAVE_FALIED);
            record.setMsg("添加key异常！");
            e.printStackTrace();
        } finally {
            record.setData(count);
            return record;
        }
    }

    @Override
    public Record findNameBykeyId(String attributeName) {
        Record record = new Record();
        int count = 0;
        try {
            count = attributeKeyMapper.findNameBykeyId(attributeName);
            if (count > 0) {
                record.setStatus(Status.SELECT_SUCCESS);
                record.setMsg("查询keyId成功！");
                record.setData(count);
            } else {
                record.setStatus(Status.SELECT_FALIED);
                record.setMsg("查询keyId失败！");
            }
        } catch (Exception e) {
            record.setStatus(Status.SELECT_FALIED);
            record.setMsg("查询keyId异常！");
            e.printStackTrace();
        } finally {
            return record;
        }
    }

    @Override
    public Record findkeyBytypeId(Integer typeId) {
        Record record = new Record();
        try {
            List<AttributeKey> list = attributeKeyMapper.findkeyBytypeId(typeId);
            if (null != list && list.size() != 0) {
                record.setStatus(Status.SELECT_SUCCESS);
                record.setMsg("查询key成功！");
                record.setData(list);
            } else {
                record.setStatus(Status.SELECT_FALIED);
                record.setMsg("查询key失败！");
            }
        } catch (Exception e) {
            record.setStatus(Status.SELECT_FALIED);
            record.setMsg("查询key异常！");
            e.printStackTrace();
        } finally {
            return record;
        }
    }


    /**
     * 查询规格集合
     *
     * @param goodsId     商品id
     * @param keyId       规格所属id  例如: 颜色 1
     * @param attributeId 规格id  例如：黑色 2
     * @return
     */
    @Override
    public Record findSpecsListByGoodsId(Integer goodsId, Integer keyId, Integer attributeId) {
        Record record = new Record();
        List<AttributeKey> list = null;
        try {
            list = attributeKeyMapper.findSpecsListByGoodsId(goodsId, keyId, attributeId);
        } catch (Exception e) {
            e.printStackTrace();
            record.setMsg("查询规格集合异常!!!");
            record.setStatus(Status.SELECT_FALIED);
            return record;
        }
        if (null != list && list.size() > 0) {
            record.setMsg("查询规格成功!");
            record.setStatus(Status.SELECT_SUCCESS);
            record.setData(list);
        } else {
            record.setMsg("查询规格失败！！");
            record.setStatus(Status.SELECT_SUCCESS);
        }
        return record;
    }


}
